//
//  HRViewController.m
//  InviteJob
//
//  Created by LangLang on 17/10/26.
//  Copyright © 2017年 Shui. All rights reserved.
//

#import "HRViewController.h"
#import "RecruitmentDetailViewController.h"
#import "HRTableViewCell.h"


static NSString *setCell = @"setCell";

@interface HRViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,assign)  NSInteger page;
@property (nonatomic,strong) NSString *perpage;
@property (nonatomic,strong) NSMutableArray *dataArr;

@property (nonatomic, strong) UIButton *navRightButton;
@property (nonatomic,strong) UITableView *tableView;

@end

@implementation HRViewController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self postData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    TitleView(@"我要招人")
    // right item
    self.navRightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    [self.navRightButton setImage:[UIImage imageNamed:@"icon_nav_public_add"] forState:UIControlStateNormal];
    self.navRightButton.imageEdgeInsets = UIEdgeInsetsMake(0,15, 0, -15);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:self.navRightButton];
    self.navigationItem.rightBarButtonItem = rightItem;
    // 事件绑定
    [self.navRightButton addTarget:self action:@selector(navAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    _page = 1;
    self.perpage = @"10";
    [self postData];
    
     self.tableView.hidden = NO;
    
    __unsafe_unretained UITableView *tableView = self.tableView;
    tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _page = 1;
        [self postData];
    }];
    [tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _page++;
        [self postData];
    }];
    self.tableView.mj_footer.ignoredScrollViewContentInsetBottom = 30;
    
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        self.dataArr = @[].mutableCopy;
    }
    return _dataArr;
}

- (void)navAction {
    RecruitmentDetailViewController *recrui = [RecruitmentDetailViewController new];
    [self.navigationController pushViewController:recrui animated:YES];
}


-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellEditingStyleNone;
        _tableView.rowHeight = 95;
        [self.view addSubview:_tableView];
        
        [_tableView registerNib:[UINib nibWithNibName:@"HRTableViewCell" bundle:nil] forCellReuseIdentifier:setCell];
        
    }
    return _tableView;
}

#pragma mark  - 代理 -
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HRTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:setCell];
    BaseModel *model = _dataArr[indexPath.row];
    
    cell.titles.text = model.cate_name;
    cell.price.text = model.salary;
    cell.msg.text = [NSString stringWithFormat:@"%@%@    %@    %@",model.city_name,model.county_name,model.work_experience,model.education];
    cell.time.text = model.update_time;
    
    return cell;
}

#pragma mark -- 删除 --
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return   UITableViewCellEditingStyleDelete;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIAlertController *actionSheetController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"确定要删除该招聘信息吗？"  preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *determineAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        _page = 1;
        self.perpage = @"10";
        BaseModel *model = _dataArr[indexPath.row];
        [self postDeletDataZpId:model.zp_id];
        [self.dataArr removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
    }];
    
    [actionSheetController addAction:determineAction];
    [actionSheetController addAction:cancelAction];
    
    [self presentViewController:actionSheetController animated:YES completion:nil];
}
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
- (BOOL)tableView: (UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
    return NO;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    BaseModel *model = _dataArr[indexPath.row];
    RecruitmentDetailViewController *detail = [RecruitmentDetailViewController new];
    detail.zp_id = model.zp_id;
    [self.navigationController pushViewController:detail animated:YES];
    
}

-(void)postData{
    isUser
     NSString *strUrl = [NSString stringWithFormat:post_employ_getLists_api];
    
    NSDictionary *dicParameter = @{@"user_id":user.user_id,
                                   @"token":user.token,
                                   @"page":[NSString stringWithFormat:@"%ld",_page],
                                   @"perpage":self.perpage};
    
    [PostNetWorking postNetWorkingStr:strUrl andparameters:dicParameter Success:^(id responseObject) {
        
        [self noData:_page ReqresponseObject:responseObject lists:@"lists" dataArr:self.dataArr arrCount:responseObject[@"datas"][@"lists"] withTable:self.tableView];
        
    } failed:^(NSError *error) {
        [self endRefreshing:self.tableView];
    }];
}

// 加载无数据显示和加载数据


-(void)noData:(NSInteger)page ReqresponseObject:(id)responseObject lists:(NSString *)lists dataArr:(NSMutableArray *)dataArr  arrCount:(NSArray *)arrCount withTable:(UITableView *)DefineTableView{
    __unsafe_unretained UITableView *tableView = DefineTableView;
    
    int code = [responseObject[@"code"]intValue];
    
    if (code == 200) {
        if (page==1) {
            if (dataArr.count) {
                [dataArr removeAllObjects];
            }
            self.dataArr = [BaseModel parsingDateFromResultDict:responseObject andData:lists];
            
        }else {
            [dataArr addObjectsFromArray:[BaseModel parsingDateFromResultDict:responseObject andData:lists]];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            if (arrCount.count>0) {
                [self hideNoDataView];
                if (arrCount.count<10) {
                    tableView.mj_footer.hidden = YES;
                }else {
                    tableView.mj_footer.hidden = NO;
                }
            }else {
                if (arrCount.count<=0) {
                    [_tableView.mj_footer setState: MJRefreshStateNoMoreData];
                    return;
                }
                [self shopNoDataViewBackgroundColor:[UIColor clearColor] withImage:@"img_home_no_news" withLabelMsg:@"没有发布招聘信息" withButton:@"" isY:0];
                
            }
            [self endRefreshing:self.tableView];
            
        });
    }else{
        dataArr = [NSMutableArray array];
        [self endRefreshing:self.tableView];
    }
    
}

-(void)endRefreshing:(UITableView *)DefineTableView{
    __unsafe_unretained UITableView *tableView = DefineTableView;
    [tableView.mj_header endRefreshing];
    [tableView.mj_footer endRefreshing];
    [tableView reloadData];
    
}

#pragma mark -- 删除 --
-(void)postDeletDataZpId:(NSString *)zp_id{
    isUser
    NSString *strUrl = [NSString stringWithFormat:post_delEmploy_api];
    
    NSDictionary *dicParameter = @{@"user_id":user.user_id,
                                   @"token":user.token,
                                   @"zp_id":zp_id};
    
    [PostNetWorking postNetWorkingStr:strUrl andparameters:dicParameter Success:^(id responseObject) {
        
        if ([responseObject[@"code"] intValue] == 200) {
            
            SHuiMBhudTextTimeStr(@"删除成功");
            dispatch_async(dispatch_get_main_queue(), ^{
                [self postData];
                
            });
        }else{
            SHuiMBhudTextTimeStr(responseObject[@"mark"]);
        }
        
        
    } failed:^(NSError *error) {
        
        SHuiMBhudTextTimeStr(@"网络错误");
    }];
}
@end
